package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class RequestAddGroupContract
{
    public String RequesterID ;
    public String GroupID ;
    public String Comment ;

    public RequestAddGroupContract(){}
    public RequestAddGroupContract(String requesterID,  String groupID, String comment)
    {
        this.RequesterID = requesterID;
        this.GroupID = groupID;
        this.Comment = comment;
    }

    public byte[] serialize() throws Exception {
        byte[] bComment = Comment.getBytes(StandardCharsets.UTF_8);
        byte[] bGroupID = GroupID.getBytes(StandardCharsets.UTF_8);
        byte[] bRequesterID = RequesterID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bComment.length + 4 + bGroupID.length + 4 + bRequesterID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bComment.length);
        body.writeBytes(bComment);
        body.writeInt(bGroupID.length);
        body.writeBytes(bGroupID);
        body.writeInt(bRequesterID.length);
        body.writeBytes(bRequesterID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(), 0, result, 0, result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException
    {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.Comment= SerializeUtils.readStrIntLen(buffer);
        this.GroupID=SerializeUtils.readStrIntLen(buffer);
        this.RequesterID=SerializeUtils.readStrIntLen(buffer);
    }
}
